<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="字典名称">
        <el-select v-model="formInline.dictName" placeholder="请选择字典名称">
          <el-option :label="m.dictName" :value="m.dictName" v-for="m in menus" :key="m.dictId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="字典类型">
        <el-input v-model="formInline.dictType" placeholder="字典类型"></el-input>
<!--        <el-select v-model="formInline.dictType" placeholder="字典类型">-->
<!--          <el-option-->
<!--              v-for="item in getDictTypes"-->
<!--              :key="item.dictId"-->
<!--              :label="item.dictType"-->
<!--              :value="item.dictId">-->
<!--          </el-option>-->
<!--        </el-select>-->
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="formInline.status" placeholder="状态" clearable>
          <el-option  label="正常" value="0"></el-option>
          <el-option  label="停用" value="1"></el-option>
        </el-select>
      </el-form-item><br>
      <el-form-item label="开始日期">
        <el-date-picker
            v-model="formInline.startTime"
            type="date"
            placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期">
        <el-date-picker
            v-model="formInline.endTime"
            type="date"
            placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="list">查询</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            plain
            size="mini"
            type="primary"
            @click="openAdd"
            icon="el-icon-plus"
            v-hasPermi="['system:dict:add']"
        >添加</el-button>
        <el-button
            plain
            icon="el-icon-edit"
            size="mini"
            type="success"
            @click="openUpdate(scope.row)"
            :disabled="single"
            v-hasPermi="['system:dict:edit']"
        >修改</el-button>
        <el-button
            plain
            icon="el-icon-delete"
            size="mini"
            type="warning"
            @click="remove"
            :disabled="multiple"
            v-hasPermi="['system:dict:remove']"
        >删除</el-button>
        <el-button
            plain
            icon="el-icon-download"
            size="mini"
            type="danger"
            @click="excel"
            v-hasPermi="['system:dict:export']"
        >导出</el-button>

      </el-col>
    </el-row>

<!--    <div id="app">-->
<!--      <el-tabs v-model="activeName" @tab-click="handleClick">-->
<!--        <el-tab-pane v-for="tab in tabs" :key="tab.name+'1'" :name="tab.name" :label="tab.label" >{{tab.content}}</el-tab-pane>-->
<!--      </el-tabs>-->

<!--      <el-tabs v-model="activeName" type="card" closable @tab-remove="handleRemove" @tab-click="handleClick">-->
<!--        <el-tab-pane v-for="tab in tabs" :key="tab.name+'2'" :name="tab.name" :label="tab.label" ></el-tab-pane>-->
<!--      </el-tabs>-->

<!--      <el-tabs v-model="activeName" type="border-card" editable @tab-click="handleEdit">-->
<!--        <el-tab-pane v-for="tab in tabs" :key="tab.name+'3'" :name="tab.name" :label="tab.label" >{{tab.content}}</el-tab-pane>-->
<!--      </el-tabs>-->
<!--      <el-radio-group v-model="position">-->
<!--        <el-radio label="top">top</el-radio>-->
<!--        <el-radio label="bottom">bottom</el-radio>-->
<!--        <el-radio label="left">left</el-radio>-->
<!--        <el-radio label="right">right</el-radio>-->
<!--      </el-radio-group>-->
<!--      <el-tabs v-model="activeName" type="border-card" :tab-position="position">-->
<!--        <el-table-pane v-for="tab in tabs" :key="tab.name+'3'" :name="tab.name">-->
<!--          <template slot="label">-->
<!--            <i class="el-icon-search">{{tab.label}}</i>-->
<!--          </template>-->
<!--          {{tab.content}}-->
<!--        </el-table-pane>-->
<!--      </el-tabs>-->
<!--    </div>-->
    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          fixed
          prop="dictId"
          label="字典主键"
          width="150">
      </el-table-column>
      <el-table-column
          prop="dictName"
          label="字典名称"
          width="120">
      </el-table-column>
      <el-table-column
          prop="dictType"
          label="字典类型"
          width="300">
<!--        <template slot-scope="s">-->
<!--          <el-button @click="datalist(s.row.dictType)" type="text">{{ s.row.dictType }}</el-button>-->
<!--        </template>-->
        <template v-slot="s">
          <router-link :to="{ path: '/dictData', query: { dictType: s.row.dictType } }" class="link-type">
            <span>{{ s.row.dictType }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column
          prop="status"
          label="状态"
          width="120">
        <template v-slot="s">
          <!--          {{s.row.status==0?'正常':'停用'}}-->
          <el-tag v-if="s.row.status==0">正常</el-tag>
          <el-tag v-if="s.row.status==1" type="danger">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="pic"
          label="图片"
          width="200">
        <template v-slot="s">
          <el-image
              style="width: 100px;height: 100px"
              :src="s.row.pic"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
          prop="remark"
          label="备注"
          width="150">
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间"
          width="150">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="120">
        <template slot-scope="scope">
          <el-button class="el-icon-edit" @click="openUpdate(scope.row)" type="text" size="small">修改</el-button>
          <el-button class="el-icon-delete" @click="del(scope.row.dictId)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加字典" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="字典名称" :label-width="formLabelWidth" prop="dictName">
          <el-input v-model="form.dictName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典类型" :label-width="formLabelWidth" prop="dictType">
          <el-input v-model="form.dictType" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="岗位状态" :label-width="formLabelWidth">
          <el-radio v-model="form.status" label="0">正常</el-radio>
          <el-radio v-model="form.status" label="1">停用</el-radio>
        </el-form-item>
        <el-form-item label="图片上传" :label-width="formLabelWidth">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:8100/system/post/addPic"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form.remark">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改岗位" :visible.sync="dialogFormVisible1">
      <el-form :model="form1">
        <el-form-item label="字典名称" :label-width="formLabelWidth1">
          <el-input v-model="form1.dictName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典类型" :label-width="formLabelWidth1">
          <el-input v-model="form1.dictType" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="岗位状态" :label-width="formLabelWidth1">
          <el-radio v-model="form1.status" label="0">正常</el-radio>
          <el-radio v-model="form1.status" label="1">停用</el-radio>
        </el-form-item>
        <el-form-item label="图片上传" :label-width="formLabelWidth">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:8100/system/post/addPic"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth1">
          <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form1.remark">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "DictView",
  data() {
    return {
      menus:[],
      formInline:{},
      tableData:[],
      options: [{
        value: '选项1',
        label: '正常'
      }, {
        value: '选项2',
        label: '停用'
      }],
      value: '',
      single:true,
      multipleSelection:[],
      ids:[],
      multiple:true,
      form:{},
      dialogFormVisible: false,
      formLabelWidth: '120px',
      dialogFormVisible1: false,
      formLabelWidth1: '120px',
      form1:{},
      rules: {
        dictName: [
          {required: true, message: '请输入字典名称', trigger: 'blur'},
        ],
        dictType: [
          {required: true, message: '请输入字典类型', trigger: 'blur'},
        ],
      },
      activeName: 'first',
      tabs:[
          {name:'first',label:'字典类型',content:'1'},
          {name:'second',label:'字典数据',content:'2'},
          {name:'third',label:'字典数据',content:'3'},
          {name:'fouth',label:'字典数据',content:'4'},
      ],
      // position:'',
      imageUrl:'',
    }
  }, methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.pic=res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    // getDictType(){
    //   this.axios.post("/system/dictType/getDictType").then(res=>{
    //     this.getDictTypes=res.data.data;
    //   })
    // },

    reset(){
      window.location.reload();
    },
    getromus()
    {
      let dictType = this.$router.query.dictType;

      this.formInline.dictType = dictType;
    },
    getxlk() {
      this.axios.post("/system/dictType/getxlk").then(res=>{
        this.menus=res.data.data;
      })
    },
    handleClick(target, action) {
      console.log(target.name, action);
    },
    handleRemove(targeName){
      this.tabs.forEach((item,index)=>{
        if (item.name===targeName){
          this.tabs.splice((index,1))
        }
      })
    },
    handleEdit(targetName,action){
      if(action==='remove'){
        this.tabs.forEach((item,index)=>{
          if (item.name===targetName){
            this.tabs.splice(index,1)
          }
        })
      }else if(action==='add'){
        let addItem={
          name:"newName"+this.tabs.length,
          label:"newLabel"+this.tabs.length,
          content:"newContent"+this.tabs.length
        }
        this.tabs.push(addItem)
      }
    },
    datalist(dictType){
      window.location.href="http://localhost:8080/dictDataList?dictType="+dictType;
    },
    list(){
      this.axios.post("/system/dictType/dictList",this.formInline).then(res=>{
        this.tableData=res.data.data;
      })
    },
    openAdd(){
      this.dialogFormVisible=true;
      this.form={status:'0'};
    },
    add(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post("/system/dictType/add",this.form).then(res=>{
            if (res.data.code==200){
              this.dialogFormVisible=false;
              this.$message.success(res.data.msg);
              this.list();
            }else{
              this.$message.error(res.data.msg);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    openUpdate(row){
      this.dialogFormVisible1=true;
      this.form1=row;
      this.imageUrl=row.pic;
    },
    update(){
      this.axios.post("/system/dictType/update",this.form1).then(res=>{
        if (res.data.code==200){
          this.dialogFormVisible1=false;
          this.$message.success(res.data.msg);
          this.list();
        }else{
          this.$message.error(res.data.msg);
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      for (let i = 0; i < val.length; i++) {
        this.ids[i]=val[i].dictId;
      }
      this.single=val.length!=1;
      this.multiple=!val.length;
    },
    remove(){
      this.axios.post("/system/dictType/deletes?ids="+this.ids).then(res=>{
        if (res.data.code==200){
          this.$message.success("删除成功");
          this.list();
        }else{
          this.$message.error("删除失败");
        }
      })
    },
    // xiugai(row){
    //   this.dialogFormVisible1=true;
    //   this.form1=row;
    //   this.list();
    // },
    excel(){
      window.location.href=this.axios.defaults.baseURL+"system/dictType/export";
    },
    del(dictId){
      this.axios.post("/system/dictType/delete?dictId="+dictId).then(res=>{
        if (res.data.code==200){
          this.$message.success(res.data.msg);
          this.list();
        }else{
          this.$message.error(res.data.msg);
        }
      })
    }
  }, created() {
    this.list();
    this.getromus();
    this.getxlk();
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>